<?php
namespace App\Http\Controllers\AppApi\My;

use App\Services\Admin\OrderBaleService;
use App\Services\Admin\OrderService;
use App\Services\Admin\OrderTempService;
use App\Services\Admin\CompanyService;
use Illuminate\Http\Request;

class OrderController extends BaseController
{
    /**
     * 订单接口
     * 制作者：九哥
     */

    protected $service;
    protected $bale;
    protected $order;
    protected $company;

    public function __construct()
    {
        parent::__construct();
        $this->service = new OrderTempService();
        $this->bale = new OrderBaleService();
        $this->order = new OrderService();
        $this->company = new CompanyService();
    }

    /**
     * 本公司货单列表
     */
    public function getComOrderList(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $models = $this->bale->getIndexByFac($uid);
        if (!count($models)) { return $this->toJson(-3); }
        $dataArr = $models;
        return $this->toJson(0,$dataArr);
    }

    /**
     * 本公司货单，参与工厂
     */
    public function getComShow(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $bale = $request->bale;
        if (!$token || !$bale) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $baleObj = $this->bale->getOne($bale);
        if (!$baleObj) { return $this->toJson(-3); }
        $temps = $this->service->getListByBale($bale);
        $dataArr = array(
            'bale' => $baleObj,
            'temps' => $temps,
        );
        return $this->toJson(0,$dataArr);
    }

    /**
     * 本公司货单，选中工厂
     */
    public function setComPartner(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $fid = $request->fid;
        if (!$token || !$fid) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $resPartner = $this->service->setPartner($uid,$fid);
        if ($resPartner['code']==-1) { return $this->toJson(-19); }
        if ($resPartner['code']==-2) { return $this->toJson(-20); }
        if ($resPartner['code']==-3) { return $this->toJson(-21); }
        if ($resPartner['code']==-4) { return $this->toJson(-22); }
        return $this->toJson(0);
    }

    /**
     * 公司，生产中订单
     */
    public function getComOrderMake(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $models = $this->order->getIndexByCid($uid);
        if (!count($models)) { return $this->toJson(-3); }
        $dataArr = $models;
        return $this->toJson(0,$dataArr);
    }

    /**
     * 公司，生产跟单
     */
    public function getComTrack(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $order = $request->orderId;
        if (!$token) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->order->getOne($order);
        if (!$model) { return $this->toJson(-3); }
        $dataArr = $model;
        return $this->toJson(0,$dataArr);
    }

    /**
     * 公司，跟单报告，获取
     */
    public function getComCheck(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $order = $request->orderId;
        if (!$token) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->order->getComCheck($order);
        if (!$model) { return $this->toJson(-3); }
        $dataArr = $model;
        return $this->toJson(0,$dataArr);
    }

    /**
     * 公司，跟单报告，设置
     */
    public function setComCheck(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $order = $request->orderId;
        if (!$token) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $stylelist = $request->stylelist;
        $sizelist = $request->sizelist;
        $thrumlist = $request->thrumlist;
        $detail = $request->detail;
        $staffTotalNum = $request->staffTotalNum;
        $staffStyleNum = $request->staffStyleNum;
        $finishTime = $request->finishTime;
        $styleArr = array();
        $sizeArr = array();
        $thrumArr = array();
        foreach ($stylelist as $k=>$v) {
            if ($stylelist[$k]) {
                $styleArr[$k] = $stylelist[$k];
            } else {
                $styleArr[$k] = '';
            }
        }
        foreach ($sizelist as $k2=>$v2) {
            foreach ($v2 as $kk2=>$vv2) {
                if ($vv2) {
                    $sizeArr[$k2][$kk2] = $vv2;
                } else {
                    $sizeArr[$k2][$kk2] = '';
                }
            }
        }
        foreach ($thrumlist as $k3=>$v3) {
            foreach ($v3 as $kk3=>$vv3) {
                if ($kk3==0 || $vv3) {
                    $thrumArr[$k3][$kk3] = $vv3;
                } else {
                    $thrumArr[$k3][$kk3] = '';
                }
            }
        }
        $detail = $detail ? $detail : '';
        $staffTotalNum = $staffTotalNum ? $staffTotalNum : 0;
        $staffStyleNum = $staffStyleNum ? $staffStyleNum : 0;
        $finishTime = $finishTime ? strtotime($finishTime) : 0;
        $dataArr = array(
            'stylelist' => json_encode($styleArr),
            'sizelist' => json_encode($sizeArr),
            'thrumlist' => json_encode($thrumArr),
            'detail' => $detail,
            'staffTotalNum' => $staffTotalNum,
            'staffStyleNum' => $staffStyleNum,
            'finishTime' => $finishTime,
        );
//        return array('code'=>-1,'data'=>$dataArr);
        $resCheck = $this->order->setComCheck($order,$dataArr);
        if (!$resCheck) { return $this->toJson(-3); }
        return $this->toJson(0);
    }

    /**
     * 公司，确认验收报告
     */
    public function setComCheckSure(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $order = $request->orderId;
        if (!$token) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $model = $this->order->getOne($order);
        if (!$model) { return $this->toJson(-3); }
        if (!in_array($model->status,[2,3])) {
            return $this->toJson(-22);
        }
        $this->order->setComCheckSure($order);
        return $this->toJson(0);
    }

    /**
     * 工厂抢单，公司货单
     */
    public function getFacOrderList(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $models = $this->bale->getIndexByFac(0);
        if (!count($models)) { return $this->toJson(-3); }
        $dataArr = $models;
        return $this->toJson(0,$dataArr);
    }

    /**
     * 工厂抢单，货单详情
     */
    public function getFacShow(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $bale = $request->bale;
        if (!$token) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $baleObj = $this->bale->getOne($bale);
        $models = $this->bale->getIndexByFac(0);
        $temps = $this->service->getListByBale($bale);
        $resTemp = $this->service->getOneByBale($bale);
        $factory = $this->company->getOneByUid($uid);
        if (!$baleObj || !count($models) || !$factory) {
            return $this->toJson(-3);
        }
        if ($resTemp['code']==-1) { return $this->toJson(-19); }
        if ($resTemp['code']==-2) { return $this->toJson(-20); }
        $dataArr = array(
            'bale' => $baleObj,
            'temps' => $temps,
            'temp' => $resTemp['data'],
            'factory' => $factory,
        );
        return $this->toJson(0,$dataArr);
    }

    /**
     * 工厂抢单，立即抢单
     */
    public function setFacOrderAdd(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $bale = $request->bale;
        if (!$token) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $resBale = $this->bale->getOne($bale);
        if (!$resBale) { return $this->toJson(-21); }
        $resTemp = $this->service->getOneByBale($bale);
        if (!$resTemp) { return $this->toJson(-20); }
        $dataArr = array(
            'bale' => $bale,
            'cid' => $resBale->uid,
            'fid' => $uid,
        );
        $this->service->add($dataArr);
        return $this->toJson(0);
    }

    /**
     * 工厂，合作中订单
     */
    public function getFacOrderMake(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $models = $this->order->getIndexByFid($uid);
        if (!count($models)) { return $this->toJson(-3); }
        $dataArr = $models;
        return $this->toJson(0,$dataArr);
    }

    /**
     * 工厂，确认生产
     */
    public function setFacOrderMakeStatus(Request $request)
    {
        if ($request->app_access!=$this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) { return $this->toJson(-13); }
        $resStatus = $this->order->setOrderMakeStatus($uid);
        if (!$resStatus) { return $this->toJson(-3); }
        return $this->toJson(0);
    }
}